avalon.validators.othersIndustry = {
	message: 'The other industry is required and must be less than 50 characters long!',
	get: function(value, field, next) {
		var companyType = $("#companyType").val();
		if((0==companyType) && (companyType != "")){
			if(value ==''){
				next(false);
			} else {
				var flag = value.length >= 50;
				next(!flag);
			}
		} else {
			next(true);
		}
		return value
	}
};
avalon.validators.postcodeV={
	message:'The postcode is required!',
	get:function(value, field, next){
		var country = $("#country option:selected").text();
		var _arr=['United Kingdom', ''];
		if(_arr.indexOf(country)!=-1) {
			if(value =='') {
				next(false);
			}else {
				var flag = value.length >= 50;
				next(!flag);
			}
		}else {
			next(true);
		}
		return value
	}
};
var editPage = avalon.define({
	$id: "editPage",
	uid: "",
	companyName: "",
	inShort: "",
	taxNo: "",
	companyType: "",
	email: "",
	region: "",
	country: "",
	state: "",
	city: "",
	detailAddress: "",
	contact: "",
	jobTitle: "",
	telCode:"",
	telNo: "",
	existingDistributor: "",
	preferredDistributor: "",
	companySize: "",
	othersIndustry:"",
	companyWebsite: "",
	aapisr: "",
	remark: "",
	firstName: "",
	lastName: "",
	postcode: "",
	cityRequired:true,
	codes:window.DAHUA_CONFIG.CODES,//国码
	chooseCompanyType: function() {
		var companyType = this.companyType;
		if( 0 == companyType) {
			this.othersIndustry = "";
			$("#i_others").removeClass("mui-hidden");
		} else {
			$("#i_others").addClass("mui-hidden");
		}
	},
	validate: {
		onValidateAll: function(reasons) {
			if(reasons.length) {
				mui.toast(reasons[0].getMessage());
			} else {
				var form=$("#editForm");
				mui.ajax('/customer/edit.do', {
					data: form.serialize(),
					dataType: 'json',
					type:'post',
					success: function(data) {
						 mui.alert("",data.msg);
					}
				});
			}
		}

	},
	edit: function(e) {
		e.preventDefault()
		this.validate.onManual();
	}
});
function initPostcode(_val) {
	var _arr=['United Kingdom', ''];
	if(_arr.indexOf(_val)!=-1) {
		$('#postcodeDiv').show();
	}else {
		$('#postcodeDiv').hide();
	}
}
mui.ready(function() {
	
	var uid = $("#uid").val();
	editPage.uid = uid;
	
	mui.ajax('/registry.do', {
		data: {
			'name':'CUSTOMER_CITY_REQUIRED'
		},
		dataType: 'json',
		type: 'post',
		success: function(data) {
			if(null!=data&&data.CUSTOMER_CITY_REQUIRED){
				if(data.CUSTOMER_CITY_REQUIRED=='1') {
					editPage.cityRequired=true;
				}else {
					editPage.cityRequired=false;
				}
			}
			initCityRequired();		
		}
	});
	mui.ajax('/customer/get.json', {
		data: {
			uid: editPage.uid
		},
		dataType: 'json',
		success: function(data) {
			editPage.companyName=data.companyName;
			editPage.inShort=data.inshort;
			editPage.taxNo=data.taxNo;
			
			var companyType=data.companyType;
			// if("System Integrator"==companyType||"Installer"==companyType||"Reseller"==companyType){
				editPage.companyType=companyType;
			// }else{
				// $("#i_others").removeClass("mui-hidden");
			// 	editPage.companyType="0";
			// 	editPage.othersIndustry=companyType;
			// }
			
			editPage.email=data.email;
			editPage.detailAddress=data.detailAddress;
			editPage.contact=data.contact;
			editPage.jobTitle=data.jobTitle;
			editPage.telNo=data.telNo;
			editPage.existingDistributor=data.existingDistributor;
			editPage.preferredDistributor=data.preferredDistributor;
			editPage.companySize=data.companySize;
			editPage.companyWebsite=data.companyWebSite;
			editPage.aapisr=data.aapisr;
			editPage.remark=data.remark;
			editPage.lastName=data.lastName;
			editPage.firstName=data.firstName;
			editPage.postcode=data.postcode;
			editPage.telCode=data.telCode;
			
			if(null!=data.regionLocation){
				editPage.region = data.regionLocation.id;
			}
			if(null!=data.countryLocation){
				editPage.country = data.countryLocation.id;
				initPostcode(data.countryLocation.name);
			}
			if(null!=data.stateLocation){
				editPage.state = data.stateLocation.id;
			}
			if(null!=data.cityLocation){
				editPage.city = data.cityLocation.id;
			}
			$("#locations").cxSelect({
				selects: ["region", "country", "state", "city"],
				required:false,
				firstTitle:$.i18n.prop("Please_choose")
			});
			if(data.integralOpen=='2'&& data.salesforceId!='') {
				$('input,select,textarea,label').each(function(i,item){
					if($(this).attr('type')=='hidden') return;
					var _span=$("<label></label>"),_parent=$(this).parent();
					_span.on('click', function(){
						mui.toast($.i18n.prop('user_edit_msg'));
					});
					if(this.tagName=='LABEL') {
						_span.text($(this).text());
					}else if(this.tagName=='SELECT') {
						_span.text($(this).find('option:selected').text());
					}else {
						_span.text($(this).val());
					}
					$(this).remove();
					_parent.append(_span);
					// $(this).attr("disabled", "disabled");
				});
				$('#save').remove();
			}
		}
	});
	
	mui('.mui-input-row').on('change', '#country',function(){
		var _val=$(this).find('option:selected').text();
		initPostcode(_val);
	});

	function initCityRequired() {
		if(editPage.cityRequired) {
			$('#city').on('cxselect-init', function(e, r, d){
				if(r) {
					$('#city_sup').show();
					$(this).attr('ms-rules', '{required: true}');
					// editPage.cityRequired=true;
				}else {
					$('#city_sup').hide();
					$(this).attr('ms-rules', '{required: false}');
					// editPage.cityRequired=false;
				}
			});
		}else {
			$('#city_sup').hide();
		}
	}

});